package com.zerone.pay.gateway;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.service.AlipayMonitorService;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayMonitorServiceImpl;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.alipay.demo.trade.service.impl.AlipayTradeWithHBServiceImpl;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by fire on 2017/8/28.
 */
@Configuration("gatewayConfiguration")
public class GatewayConfiguration {

	protected static final Logger logger = LoggerFactory
			.getLogger(GatewayConfiguration.class);

  static {
    Configs.init("zfbinfo.properties");

  }

  @Bean(name = "alipayTradeService")
  public AlipayTradeService getAlipayTradeService() {
    return new AlipayTradeServiceImpl.ClientBuilder().build();
  }

  @Bean(name = "tradeWithHBService")
  public AlipayTradeService getTradeWithHBService() {
    return new AlipayTradeWithHBServiceImpl.ClientBuilder().build();
  }

  @Bean(name = "monitorService")
  public AlipayMonitorService getMonitorService() {
    return new AlipayMonitorServiceImpl.ClientBuilder()
        .setGatewayUrl("http://mcloudmonitor.com/gateway.do").setCharset("utf-8").setFormat("json")
        .build();
  }

  @Bean(name = "alipayClient")
  public AlipayClient getAlipayClient() {
    AlipayClient alipayClient =
        new DefaultAlipayClient(Configs.getOpenApiDomain(), Configs.getAppid(),
            Configs.getPrivateKey(), "json", "utf-8", Configs.getAlipayPublicKey(), "RSA2");
    return alipayClient;
  }


  @Bean(name = "wXPayConfig")
  public WXPayConfigMore getWXPayConfig(){
	return wxConfigInit("wxinfo.properties");
  }

	/**
	 * 不使用沙箱
	 * @param wXPayConfig
	 * @return wXpay
	 */
  @Bean(name = "wXpay")
  public WXPay getWXpay(WXPayConfigMore wXPayConfig){
	  return new WXPay(wXPayConfig,wXPayConfig.getSignType(),false);
  }

	/**
	 * 使用沙箱
	 * @param wXPayConfig
	 * @return wXpayUseSandBox
	 */
	@Bean(name = "wXpayUseSandBox")
  public WXPay getWXpayUseSandBox(WXPayConfigMore wXPayConfig){
	  return new WXPay(wXPayConfig,wXPayConfig.getSignType(),true);
  }


	private WXPayConfigMore wxConfigInit(String filePath) {
		org.apache.commons.configuration.Configuration configs =null;
		try {
			configs = new PropertiesConfiguration(filePath);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		if (configs == null) {
			throw new IllegalStateException("can`t find file by path:"+ filePath);
		}
		WXPayConfigImpl wxPayConfig = new WXPayConfigImpl();
		wxPayConfig.setAppID(configs.getString("APP_ID"));
		wxPayConfig.setMchID(configs.getString("MCH_ID"));
		wxPayConfig.setHttpConnectTimeoutMs(configs.getInt("HTTP_CONNECT_TIMEOUT_MS"));
		wxPayConfig.setHttpReadTimeoutMs(configs.getInt("HTTP_READ_TIMEOUT_MS"));
		wxPayConfig.setToken(configs.getString("TOKEN"));
		wxPayConfig.setAppSecret(configs.getString("APP_SECRET"));
		wxPayConfig.setKey(configs.getString("API_KEY"));
		wxPayConfig.setSignType(WXPayConstants.SignType.valueOf(configs.getString("SIGN_TYPE")));
		return wxPayConfig;
	}
}
